package org.codeart.api_driver.controller;

import org.codeart.api_driver.service.UserService;
import org.codeart.common.dto.DriverCarBindingRelationship;
import org.codeart.common.dto.DriverUser;
import org.codeart.common.dto.DriverUserWorkStatus;
import org.codeart.common.dto.TokenResult;
import org.codeart.common.util.JwtUtils;
import org.codeart.common.util.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

@RestController
public class UserController {

    @Autowired
    private UserService userService;

    @PutMapping("/user")
    public R<Void> updateUser(@RequestBody DriverUser driverUser) {
        boolean b = userService.updateUser(driverUser);
        if (b) {
            return R.ok();
        }
        return R.fail();
    }

    @PostMapping("/driver-user-work-status")
    public R<Void> changeWorkStatus(@RequestBody DriverUserWorkStatus driverUserWorkStatus) {
        boolean b = userService.changeWorkStatus(driverUserWorkStatus);
        if (b) {
            return R.ok();
        }
        return R.fail();
    }

    /**
     * 根据司机token查询 司机和车辆绑定关系
     */
    @GetMapping("/driver-car-binding-relationship")
    public R<DriverCarBindingRelationship> getDriverCarBindingRelationship(HttpServletRequest request) {
        String authorization = request.getHeader("Authorization");
        TokenResult tokenResult = JwtUtils.checkToken(authorization);
        String driverPhone = tokenResult.getPhone();
        return userService.getDriverCarBindingRelationship(driverPhone);
    }

    @GetMapping("/work-status")
    public R<DriverUserWorkStatus> getWorkStatus(Long driverId) {
        return userService.getWorkStatus(driverId);
    }

}
